﻿using System;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using LiveCharts;
using LiveCharts.Charts;
using LiveCharts.Definitions.Points;
using LiveCharts.Dtos;
using LiveCharts.Wpf.Geometries;
using LiveCharts.Wpf;
using LiveCharts.Wpf.Points;

internal class ChartPointView : PointView, IChartPointView, IScatterPointView
{
    public double Diameter { get; set; }

    private PathGeometry pathGeometry;
    internal GeometryShape geometryShape { get; set; }

    public override void DrawOrMove(ChartPoint previousDrawn, ChartPoint current, int index, ChartCore chart)
    {
        base.ValidArea = new CoreRectangle(current.ChartLocation.X - this.Diameter / 2.0, current.ChartLocation.Y - this.Diameter / 2.0, this.Diameter, this.Diameter);
        if (base.IsNew)
        {
            this.pathGeometry = (PathGeometry)((GScatterSeries)current.SeriesView).ShapesStroke.Path_0.Data;
            this.geometryShape.Diameter = this.Diameter;
            this.geometryShape.Draw(this.pathGeometry, new Point(current.ChartLocation.X, current.ChartLocation.Y), false, TimeSpan.Zero);
            if (base.DataLabel != null)
            {
                Canvas.SetTop(base.DataLabel, current.ChartLocation.Y);
                Canvas.SetLeft(base.DataLabel, current.ChartLocation.X);
            }
        }
        this.geometryShape.Diameter = this.Diameter;
        if (chart.View.DisableAnimations)
        {
            this.geometryShape.Draw(this.pathGeometry, new Point(current.ChartLocation.X, current.ChartLocation.Y), false, TimeSpan.Zero);
            if (base.DataLabel != null)
            {
                base.DataLabel.UpdateLayout();
                double leftValue = this.FixLeft(current.ChartLocation.X - base.DataLabel.ActualWidth * 0.5, chart);
                double topValue = this.FixTop(current.ChartLocation.Y - base.DataLabel.ActualHeight * 0.5, chart);
                Canvas.SetTop(base.DataLabel, topValue);
                Canvas.SetLeft(base.DataLabel, leftValue);
            }
            return;
        }
        TimeSpan animationsSpeed = chart.View.AnimationsSpeed;
        if (base.DataLabel != null)
        {
            base.DataLabel.UpdateLayout();
            double leftValue = this.FixLeft(current.ChartLocation.X - base.DataLabel.ActualWidth * 0.5, chart);
            double topValue = this.FixTop(current.ChartLocation.Y - base.DataLabel.ActualHeight * 0.5, chart);
            base.DataLabel.BeginAnimation(Canvas.LeftProperty, new DoubleAnimation(leftValue, animationsSpeed));
            base.DataLabel.BeginAnimation(Canvas.TopProperty, new DoubleAnimation(topValue, animationsSpeed));
        }
        this.geometryShape.Draw(this.pathGeometry, new Point(current.ChartLocation.X, current.ChartLocation.Y), true, animationsSpeed);
    }

    public override void RemoveFromView(ChartCore chart)
    {
        chart.View.RemoveFromDrawMargin(base.DataLabel);
        this.geometryShape.Erase(this.pathGeometry);
    }

    protected double FixLeft(double v, ChartCore chartCore)
    {
        if (v + base.DataLabel.ActualWidth > chartCore.DrawMargin.Width)
        {
            v -= v + base.DataLabel.ActualWidth - chartCore.DrawMargin.Width;
        }
        if (v < 0.0)
        {
            v = 0.0;
        }
        return v;
    }

    protected double FixTop(double v, ChartCore chartCore)
    {
        if (v + base.DataLabel.ActualHeight > chartCore.DrawMargin.Height)
        {
            v -= v + base.DataLabel.ActualHeight - chartCore.DrawMargin.Height;
        }
        if (v < 0.0)
        {
            v = 0.0;
        }
        return v;
    }

    public override void OnHover(ChartPoint point)
    {
    }


    public override void OnHoverLeave(ChartPoint point)
    {
    }


}
